package rc.alfresco.wfDelegates;

import org.activiti.engine.delegate.DelegateExecution;
import org.alfresco.repo.workflow.activiti.BaseJavaDelegate;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;
import rc.database.connection.MyDBOperations;

import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.11.20
 * Time: 13.29
 * To change this template use File | Settings | File Templates.
 */
public class WriteDocConfData extends BaseJavaDelegate {

    private static final Logger logger = Logger.getLogger(WriteDocConfData.class);

    static final String NAMESPACE = "http://*.kada.lt/model/people/1.0";
    static final String NAMESPACEDOC = "http://appdevel?.kada.lt/model/content/1.0";
    static final QName PROP_AMN_ID = QName.createQName(NAMESPACE, "personVarId");
    static final QName PROP_DOCSUDRC_PAV = QName.createQName(NAMESPACEDOC, "docSudRcPav");
    static final QName PROP_DOCSUDRC_PAD = QName.createQName(NAMESPACEDOC, "docSudRcPad");

    private NodeService nodeService;

    public void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        ResultSet rs = null; Connection conn = null; Statement stmt = null;
        NodeRef director = (NodeRef) execution.getVariable("directorRef");
        NodeRef document = (NodeRef) execution.getVariable("documentRef");

        if (nodeService != null && director != null) {
            if (nodeService.getProperty(director, PROP_AMN_ID) != null) {
                try {
                    String amnid = nodeService.getProperty(director, PROP_AMN_ID).toString();
                    MyDBOperations db = new MyDBOperations("jussle","E4fc6b54","jdbc:oracle:thin:@kadadev.kada.lan:1521/kadadev","oracle.jdbc.OracleDriver");
                    Class.forName(db.getJdbcDriver());
                    conn = DriverManager.getConnection(db.getJdbcUrl(), db.getUserName(), db.getPassword());
                    conn.setAutoCommit(false);
                    stmt = conn.createStatement();
                    rs = stmt.executeQuery(db.getTarnNrAndPadPav(Integer.parseInt(amnid)));
                    if (rs.next()) {
                        nodeService.setProperty(document, PROP_DOCSUDRC_PAV, rs.getString(1));
                        logger.debug("PROP_DOCSUDRC_PAV: " + rs.getString(1));
                        nodeService.setProperty(document, PROP_DOCSUDRC_PAD, rs.getString(2));
                        logger.debug("PROP_DOCSUDRC_PAD: " + rs.getString(2));
                    } else {
                        logger.debug("DATABASE RESPONSE WITHOUT DATA");
                    }

                }
                catch (ClassNotFoundException ex) {logger.debug(ex);}
                catch (SQLException ex) {logger.debug(ex);}
                finally {
                    if (rs != null) {
                        try {rs.close();}
                        catch (SQLException e) {logger.debug(e);}
                    }
                    if (stmt != null) {
                        try {stmt.close();}
                        catch (SQLException e) {logger.debug(e);}
                    }
                    if (conn != null) {
                        try {conn.close();}
                        catch (SQLException e) {logger.debug(e);}
                    }
                }
            } else {logger.debug("Director property does not exist");}
        } else {logger.debug("NodeService or director is null");}
    }
}
